const path = require("path");
const fs = require("fs");
const webpack = require('webpack');
const node_modules_dir = path.resolve(__dirname, 'node_modules');
const htmlWebpackPlugin = require("html-webpack-plugin");
const setConfig = require("./webpack.base.config");

const pages = require("./pages")
// 获取指定路径下的入口文件
function getEntries(globPath) {
    const entries = {};
    pages.forEach(function (page) {
        page.active && (entries[page.name] = './src/entry/' + page.name);
    })
    return entries;
}

const entries = getEntries('src/entry')
const entryList = {}

Object.keys(entries).forEach(function (name) {
    // 每个页面生成一个entry，如果需要HotUpdate，在这里修改entry
    entryList[name] = ['webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', 'whatwg-fetch', 'babel-polyfill', entries[name]];
})

entryList.lib = ['react', 'react-dom', 'echarts', 'redux', 'react-router',
    'react-redux', 'classnames', 'react-router-dom'];
const baseConfig = setConfig({
    _DEV_: true,
    entry: entryList,
    plugins: [
        new webpack.optimize.CommonsChunkPlugin(
            {
                name: 'lib',
                filename: 'react.js'
            }
        ),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('development')
        }),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(),
        ...Object.keys(entries).map(function (name) {
            return new htmlWebpackPlugin({
                // 生成出来的html文件名
                filename: name + '.html',
                // 每个html的模版，这里多个页面使用同一个模版
                template: './app.html',
                // 自动将引用插入html
                inject: true,
                // 每个html引用的js模块，也可以在这里加上vendor等公用模块
                chunks: [name, 'lib'],
            });
        })
    ]
})
/*css热替换*/

// baseConfig.module.rules[1] = {
//     test: /\.css$/,
//     use: ['style-loader', {
//         loader: 'css-loader',
//         options: {sourceMap: true}
//     }, 'postcss-loader']
// }
//
// baseConfig.module.rules[2] = {
//     test: /\.scss$/,
//     use: ['style-loader', {
//         loader: 'css-loader',
//         options: {sourceMap: true}
//     }, 'postcss-loader','sass-loader']
// }
// baseConfig.plugins.shift()

module.exports = baseConfig